import requests
from requests.auth import HTTPBasicAuth

# RabbitMQ 管理 API 的基本设置
rabbitmq_host = "http://10.5.1.48:15672"
username = "cloud"
password = "7#BD2CFc3TBV}7m7z"

# 获取所有队列的信息
response = requests.get(
    f"{rabbitmq_host}/api/queues", auth=HTTPBasicAuth(username, password)
)
queues = response.json()

# 过滤出没有消费者的队列
queues_without_consumers = [q["name"] for q in queues if q["consumers"] == 0]

print(queues_without_consumers)

# 删除这些队列
for queue_name in queues_without_consumers:
    delete_response = requests.delete(
        f"{rabbitmq_host}/api/queues/%2F/{queue_name}",
        auth=HTTPBasicAuth(username, password),
    )
    if delete_response.status_code == 204:
        print(f"队列 {queue_name} 已被删除")
    else:
        print(f"删除队列 {queue_name} 失败: {delete_response.text}")
